<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\BaseController;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class PrintReceiptsController extends BaseController
{
    //
    public $app_secret = 'd503bc5a42286e86d7a73794d173e95d';

    const USUHTTP = 'https://open-api.ushengyun.com/printer/print';
    public $app_id = '10316';
    public $device_id = '70006979';
    public $device_secret = 'nf2z2sv3';
    public $printdata = '<MC>4</MC><S1><C>#15線下訂單</C></S1><C>*線下支付*</C>********************************<C>--线下支付--</C><S1>备注：</S1><RN>下单时间:%s<RN>订单编号:%s<RN>********************************<C>-----------1号口袋-----------</C><S1><TR><TD>%s</TD><TD>×1</TD><TD>26</TD></TR><TR><TD>%s</TD><TD>×1</TD><TD>39</TD></TR></S1>--------------其他--------------餐盒费:                 ￥2<RN>配送费:                 ￥2<RN>折扣:                   ￥27<RN>********************************            原价:69<RN>            总价:<S2>42</S2><RN><S1>美年广场C座</S1><RN>5194<RN>c(女士)(门店新客户)<RN>虚拟号码 13647975167_5194，手机号 177****3962 <RN>';

    public function index()
    {
        $data=DB::table('order')->where('pay_mode',3)->get();

        foreach($data as $item){
            // dd(json_decode($item->product_item)[0]->name);
            $protocol= array_merge([
                'appid'=>$this->app_id,
                'timestamp'=>time(),
            ],[
                'deviceid'=>$this->device_id,
                'devicesecret' => $this->device_secret,
               'printdata'=>sprintf($this->printdata,date('Y-m-d H:s',$item->created_at),$item->order_no,json_decode($item->product_item)[0]->name,json_decode($item->product_item)[0]->name)

            ]);
            $protocol['sign']=  $this->generate_signature($protocol);
    
           return $this->success($this->post(self::USUHTTP,$protocol));
        }
        // dd(sprintf($this->printdata,11,'hahah哈哈',213,23131));

        // dd(sprintf($this->printdata,$num,$location));
    //    $protocol= array_merge([
    //         'appid'=>$this->app_id,
    //         'timestamp'=>time(),
    //     ],[
    //         'deviceid'=>$this->device_id,
    //         'devicesecret' => $this->device_secret,
    //        'printdata'=>$this->printdata
    //     ]);
    //     $protocol['sign']=  $this->generate_signature($protocol);

    //    dd($this->post(self::USUHTTP,$protocol));

    }

    private function post($url, $data)
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Expect:'));
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        $response = curl_exec($ch);
        if (curl_errno($ch)) {
            throw new \Exception(curl_error($ch));
        }

        return $response;
    }


    private function generate_signature($protocol)
    {
        $stringtoSigned = '';

        ksort($protocol);

        foreach ($protocol as $k => $v) {
            if (strlen($v) > 0) {
                $stringtoSigned .= $k . $v;
            }

        }

        $stringtoSigned .= $this->app_secret;

        return md5($stringtoSigned);
    }
}
